package qdu.guisockets.clientlet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ClientUI extends JFrame{
    private JPanel subp; 
	private JTextArea tf1,tf2;
	private JButton btn;
    private JScrollPane scrollPane;
    private TestSocketClient tsc;
    public ClientUI(TestSocketClient tsc){
        this.tsc = tsc;
        tf1 = new JTextArea();
        scrollPane = new JScrollPane(tf1);
        tf2 = new JTextArea();
        btn = new JButton("发送");
        subp = new JPanel(new BorderLayout());

        new ClientMessageReceiver().start();
    }
    class ClientMessageReceiver extends Thread{
        public void run(){
            try {
                while(tsc.getSocket()!=null && !tsc.getSocket().isClosed()){
                    String msgFromServer = "";
                    msgFromServer = tsc.getMessageFromServer();
                    if(!"".equals(msgFromServer))
                        ClientUI.this.tf1.append(msgFromServer + '\n');
                }
            } catch (Exception e) {
                System.out.println("接收消息失败"); //e.printStackTrace();
            }
        }
    }
    public void initUI() {
        this.setPreferredSize(new Dimension(800, 600));
        this.setTitle("客户端");
        tf1.setEditable(false);
        
        tf1.setBackground(Color.ORANGE);
        tf2.setBackground(Color.YELLOW);
        subp.setPreferredSize(new Dimension(600,100));
        subp.add(tf2, BorderLayout.CENTER); subp.add(btn, BorderLayout.EAST);

        this.add(scrollPane, BorderLayout.CENTER);
        this.add(subp, BorderLayout.SOUTH);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.pack();     
        this.setVisible(true);
    
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendLogic();
            }
        });
        tf2.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                switch(e.getKeyCode()){
                    case KeyEvent.VK_ENTER :
                        System.out.println("enter pressed......");
                        sendLogic();
                        break;
                }
            }
        });
            
        ClientUI.this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                if(tsc!=null)
                    tsc.disconnectFromServer();
                System.exit(0);
            }
        });
    }

    private void sendLogic(){
        String msgToSend = tf2.getText();
        if(msgToSend != null && !"".equals(msgToSend.trim())){
            tf1.append("----> 我说: " + msgToSend.trim()+"\n");
            tf2.setText("");
            if(tsc != null)
                tsc.sendMessageToServer(msgToSend.trim());
        }
    }
}
